package com.upc.tp.timeplanning.controller;


import com.upc.tp.timeplanning.entity.ResBody;
import com.upc.tp.timeplanning.entity.Student;
import com.upc.tp.timeplanning.mapper.StudentMapper;
import com.upc.tp.timeplanning.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/student", method = {RequestMethod.POST, RequestMethod.GET})
public class StudentController {

    @Autowired
    StudentMapper studentMapper;

    @RequestMapping("/getAll")
    public List<Student> getAll(){
        List<Student> list = studentMapper.getAll();
        return list;
    }

//    @RequestMapping("/getByUsername")
//    public List<Student> getByUsername(@RequestBody Student stu){
//
//        List<Student> list = studentMapper.getByUsername(stu.getUsername());
//
//        return list;
//    }

    @RequestMapping("/login")
    public ResBody login(@RequestBody Student stu){

        ResBody resBody;
        try{
            Student stu_temp = studentMapper.getByUsername(stu.getUsername());
            String pwd_temp = stu_temp.getPassword();
            if(pwd_temp.equals(stu.getPassword())){
//                Map<String,String> map=new HashMap<>();   //用于生成token，暂时用不着
//                map.put("username",stu_temp.getUsername());
//                String token= JwtUtil.getToken(map);
//                Map<String,String> resMap=new HashMap<>();
//                resMap.put("token",token);
                resBody = new ResBody(100,"success",null);
            }
            else{
                resBody = new ResBody(0,"fail",null);
            }
        }catch (Exception e){
            return new ResBody(1,"no_username",null);
        }
        return resBody;
    }


    @RequestMapping("/register")
    public ResBody insert(@RequestBody Student stu){

        ResBody res;
        try{
            studentMapper.insert(stu.getUsername(),stu.getPassword());
            res = new ResBody(100,"success",null);
        }catch (Exception e){
            return new ResBody(0,"fail",null);
        }
        return res;
    }

}
